﻿using System;
using System.Runtime.InteropServices;

namespace LDSModel.Item
{
    /// <summary>
    /// 记录实体
    /// </summary>
    [StructLayout(LayoutKind.Explicit)]
    public class RecordItem
    {
        #region 变量

        [FieldOffset(0)]
        UInt32 alarmCount;

        [FieldOffset(4)]
        UInt32 ch1Value;

        [FieldOffset(8)]
        UInt32 ch2Value;

        [FieldOffset(12)]
        UInt16 year = 2012;

        [FieldOffset(14)]
        UInt16 month = 1;

        [FieldOffset(16)]
        byte day = 1;

        [FieldOffset(17)]
        byte hour;

        [FieldOffset(18)]
        byte minute;

        [FieldOffset(19)]
        byte second;

        #endregion

        /// <summary>
        /// 雷击时间
        /// </summary>
        public DateTime AlarmTime
        {
            get
            {
                if (year > 9999) year = 2012;
                if (month < 1 || month > 12) month = 1;
                if (day < 1 || day > 31) day = 1;
                if (hour > 24) hour = 0;
                if (minute > 59) minute = 0;
                if (second > 59) second = 0;

                return new DateTime(year, month, day, hour, minute % 60, second % 60);
            }
            set
            {
                year = (ushort)value.Year;
                month = (ushort)value.Month;
                day = (byte)value.Day;
                hour = (byte)value.Hour;
                minute = (byte)value.Minute;
                second = (byte)value.Second;
            }
        }

        /// <summary>
        /// 雷击次数
        /// </summary>
        public UInt32 AlarmCount
        {
            get
            {
                return Convert.ToUInt32(alarmCount);
            }
            set
            {
                alarmCount = value;
            }
        }

        /// <summary>
        /// 端口A电流值
        /// </summary>
        public float Ch1Value
        {
            get
            {
                return BitConverter.ToSingle(BitConverter.GetBytes(ch1Value), 0);
            }
            set
            {
                ch1Value = BitConverter.ToUInt32(BitConverter.GetBytes(value), 0);
            }
        }

        /// <summary>
        /// 端口B电流值
        /// </summary>
        public float Ch2Value
        {
            get
            {
                return BitConverter.ToSingle(BitConverter.GetBytes(ch2Value), 0);
            }
            set
            {
                ch2Value = BitConverter.ToUInt32(BitConverter.GetBytes(value), 0);
            }
        }
    }
}
